package com.zoweunion.mechanic.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.zoweunion.mechanic.service.NeedLeaseService;
import com.zoweunion.mechanic.util.Orgin;
import com.zoweunion.mechanic.util.task.MQDataAlarmPush;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zoweunion.mechanic.controller.BaseController;
import com.zoweunion.mechanic.util.constants.Constants;
import com.zoweunion.mechanic.util.MyException;
import com.zoweunion.mechanic.service.LeaseService;
import com.zoweunion.mechanic.entity.LeaseEntity;


import com.zoweunion.mechanic.model.ResponseBean;
import com.github.pagehelper.PageInfo;

import javax.servlet.http.HttpServletRequest;


/**
 * 出租
 *
 * @author xp
 * @email 17688825585@163.com
 * @date 2021-04-19 19:03:43
 */
@RestController
@RequestMapping("/lease")
public class LeaseController extends BaseController {
    @Autowired
    private LeaseService leaseService;
    @Autowired
    private NeedLeaseService needLeaseService;

    /**
     * 列表
     */
    @GetMapping("/leaseList")
    public ResponseBean leaseList(@RequestParam Map<String, Object> params, HttpServletRequest request) throws MyException {
        params.put("s_id", getCurrentUser(request).getS_id());
        params.put("user_id", getCurrentUser(request).getId());
        Map<String,Object> info = leaseService.queryPage(params);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", info);
    }

    /**
     * 个人列表
     */
    @GetMapping("/ownLeaseList")
    public ResponseBean ownLeaseList(@RequestParam Map<String, Object> params, HttpServletRequest request) throws MyException {
        params.put("s_id", getCurrentUser(request).getS_id());
        params.put("user_id", getCurrentUser(request).getId());
        Map<String,Object> info = leaseService.ownLeaseList(params);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", info);
    }

    /**
     * 地图列表
     */
    @GetMapping("/mapList")
    public ResponseBean mapList(@RequestParam Map<String, Object> params, HttpServletRequest request) throws MyException {
        List<Object> info = leaseService.mapList(params);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", info);
    }

    /**
     * 设备名称列表
     */
    @GetMapping("/mapSearchForList")
    public ResponseBean mapSearchForList(@RequestParam Map<String, Object> params, HttpServletRequest request) throws MyException {
        List<String> info = leaseService.mapSearchForList(params);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", info);
    }

    /**
     * 详情 
     */
    @GetMapping("/leaseInfo")
    public ResponseBean leaseInfo(@RequestParam Map<String, Object> params, HttpServletRequest request) throws MyException {
        int type = Orgin.getIntValue(params, "type");
        long id = Orgin.getLongValue(params, "id");
        Object byId = null;
        if (type==1){//
            byId = leaseService.getInfoId(id);
        }else if (type==2){
            byId = needLeaseService.getInfoById(id);
        }
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功",byId);
    }

    /**
     * 新增
     */
    @PostMapping("/leaseInsertUpdate")
    public ResponseBean leaseInsertUpdate(@RequestBody LeaseEntity params,
                                          HttpServletRequest request) throws MyException {
        String id = getCurrentUser(request).getId();
        if (params.getId() != null) {
            params.setUpdateTime(new Date());
            params.setUpdateUser(id);
            if (params.getCarId()!=null&&!params.getCarId().isEmpty()){
                LeaseEntity.builder()
                        .imgSrc(params.getImgSrc())
                        .workHours(params.getWorkHours())
                        .remark(params.getRemark())
                        .price(params.getPrice())
                        .dateType(params.getDateType())
                        .leaseTime(params.getLeaseTime())
                        .mobilePhone(params.getMobilePhone())
                        .isDriver(params.getIsDriver())
                        .isLease(params.getIsLease())
                        .updateUser(id)
                        .updateTime(new Date())
                        .build();
                leaseService.updateById(params);
            }else {
                leaseService.updateById(params);
            }
        } else {
            params.setCreateUser(id);
            params.setCreateTime(new Date());
            leaseService.save(params);
        }
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", "");
    }


}
